package com.example.pt.utils;

import com.example.pt.entity.User;
import com.example.pt.entity.UserES;
import com.example.pt.mapper.UserMapper;
import com.example.pt.repository.UserESRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class UserToUserESUtils {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private UserESRepository esRepository;

    public void syncToES(User user) {
        int uid = user.getUid();

        // 转换为 ES 实体，并设置 tid（ES 会将 @Id 字段作为文档 _id）
        UserES esUser = modelMapper.map(user, UserES.class);

        System.out.println("uid/**************"+uid);
        esUser.setUid(uid); // 确保 tid 正确传递（如果映射工具未自动处理）

        // 保存到 ES 时，tid 会作为 _id
        esRepository.save(esUser);
    }
}
